home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 416_01 / pcslide / pcslide2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-15  |  9.1 KB  |  406 lines

  1. /* Displays a file of slides on a VGA Screen - version 2 */
  2. /* Written by Nigel Salt 1991                            */
  3. /******************************************************
  4. Nigel Salt
  5. 25 Lower Station Rd
  6. Crayford
  7. Kent
  8. DA1 3PY
  9. UK
  10.  
  11. Phone +44 322 553260
  12.  
  13. Email nao@cix.complink.co.uk
  14. ******************************************************/
  15.  
  16. /* This version pre-reads the slide file to facilitate
  17.    jumping up and down
  18.    Also traps last slide until Esc is pressed            */
  19.  
  20. #include <graph.h>
  21. #include <stdio.h>
  22. #include <conio.h>
  23. #include <string.h>
  24. #include <process.h>
  25. #include <stdlib.h>
  26. #include <ctype.h>
  27.  
  28. #define MAXSLIDES 1000
  29. #define MAXLINE   255
  30.  
  31. /***************/
  32. /* Global Data */
  33. /***************/
  34. static FILE *f;
  35. static short  vidmode=_VRES16COLOR;
  36. static int  fonth=43;
  37. static char fonthstr[4]="43";
  38. static char headbase[64]="bw20t'helv'h";
  39. static char headfont[64]="bw20t'helv'h43";
  40. static int  headcolor=12;
  41. static char bodybase[64]="bw20t'helv'h";
  42. static char bodyfont[64]="bw20t'helv'h43";
  43. static int  bodycolor=10;
  44. static char divide[]="════════════════════════════════════════════════════════════════════════════════════════════════";
  45. static char buff[MAXLINE+1];
  46. static char slidefile[80];
  47. static char copyright[]="Nigel Salt (c) 1991";
  48. static long slidestart[MAXSLIDES];
  49. static int  slideno=0;
  50. static int  numslides=0;
  51. static long bgcolors[16]=
  52. {
  53. _BLACK,
  54. _BLUE,
  55. _GREEN,
  56. _CYAN,
  57. _RED,
  58. _MAGENTA,
  59. _BROWN,
  60. _WHITE,
  61. _GRAY,
  62. _LIGHTBLUE,
  63. _LIGHTGREEN,
  64. _LIGHTCYAN,
  65. _LIGHTRED,
  66. _LIGHTMAGENTA,
  67. _YELLOW,
  68. _BRIGHTWHITE
  69. };
  70.  
  71. /***********************/
  72. /* Function prototypes */
  73. /***********************/
  74. void showslides(void);
  75. void copviol(void);
  76. void main(int argc,char *argv[]);
  77. void setopts(void);
  78. void userin(void);
  79. int  getslides(void);
  80.  
  81. /******************************/
  82. void main(int argc,char *argv[])
  83. /******************************/
  84. {
  85.   static char progpath[65];
  86.   char *lastslash;
  87.  
  88.   /* Find out program home directory */
  89.   lastslash=strrchr((char *)argv[0],'\\');
  90.   if (lastslash==NULL)
  91.     progpath[0]='\0';
  92.   else
  93.     {
  94.     *(lastslash+1)='\0';
  95.     strcpy(progpath,argv[0]);
  96.     }
  97.  
  98.   /* Register fonts */
  99.   strcpy(buff,progpath);
  100.   strcat(buff,"*.fon");
  101.   if (! _registerfonts((char far *)buff))
  102.     {
  103.     fprintf(stderr,"\nNo '.fon' files found");
  104.     exit(1);
  105.     }
  106.  
  107.   /* Check copyright */
  108.   if (copyright[0]!='N')
  109.     copviol();
  110.  
  111.   /* Get slide file name if any */
  112.   if (argc>1)
  113.     strcpy(slidefile,argv[1]);
  114.   else
  115.     strcpy(slidefile,"demo.sld");
  116.  
  117.   /* Try to open slide file */
  118.   f=fopen(slidefile,"r");
  119.   if (f==NULL)
  120.     {
  121.     fprintf(stderr,"\nCannot find show file '%s'",slidefile);
  122.     exit(1);
  123.     }
  124.  
  125.   /* Find start of each slide */
  126.   numslides=getslides();
  127.   if (numslides==0)
  128.     {
  129.     fprintf(stderr,"\nCannot find any slides in file '%s'",slidefile);
  130.     exit(1);
  131.     }
  132.  
  133.  
  134.   /* Set default video mode to VGA 16 color */
  135.   vidmode=_VRES16COLOR;
  136.   if (!_setvideomode(vidmode))
  137.     vidmode=0;
  138.   slidestart[0]=0L;
  139.  
  140.   /* Get first line of slide show file */
  141.   fseek(f,0,SEEK_SET);
  142.   fgets(buff,MAXLINE,f);
  143.   showslides();
  144.   _setvideomode(_DEFAULTMODE);
  145.   exit(0);
  146. }
  147.  
  148. /******************************/
  149. int getslides(void)
  150. /******************************/
  151. {
  152.   long lastline;
  153.   int slidesfound;
  154.   long curline;
  155.  
  156.   slidesfound=0;
  157.   curline=lastline=ftell(f);
  158.   while (!feof(f)&&numslides<MAXSLIDES)
  159.     {
  160.     if (fgets(buff,MAXLINE,f)==NULL)
  161.       continue;
  162.  
  163.     /* Skip any control lines */
  164.     while (buff[0]=='#'&&!feof(f))
  165.       fgets(buff,MAXLINE,f);
  166.  
  167.     /* Skip through the text lines */
  168.     while (buff[0]!='#'&&!feof(f))
  169.       {
  170.       lastline=ftell(f);
  171.       fgets(buff,MAXLINE,f);
  172.       }
  173.  
  174.     /* Record start of first line following text of previous slide */
  175.     if (curline!=lastline)
  176.       {
  177.       slidesfound++;
  178.       slidestart[slidesfound]=lastline;
  179.       }
  180.     }
  181.   return slidesfound;
  182. }
  183.  
  184. /* On entry the show file is open and set to start of slide */
  185. /* Buff contains the first line to be processed             */
  186. /******************************/
  187. void showslides()
  188. /******************************/
  189. {
  190.   int i;
  191.   int lastslide;
  192.  
  193.   if (strlen(copyright)!=19)
  194.     copviol();
  195.   while (slideno<numslides)
  196.     {
  197.     /* Save start position of current slide */
  198.     setopts();
  199.     _clearscreen(_GCLEARSCREEN);
  200.     _setfont(headfont);
  201.     _setcolor(headcolor);
  202.     _moveto(0,0);
  203.     _outgtext(divide);
  204.     _moveto(0,fonth);
  205.     _outgtext(divide);
  206.     _moveto(fonth/2,7*fonth/8);
  207.     _outgtext(buff);
  208.  
  209.     _setfont(bodyfont);
  210.     _setcolor(bodycolor);
  211.     fgets(buff,MAXLINE,f);
  212.     for (i=0;i<8&&buff[0]!='#'&&!feof(f);i++)
  213.       {
  214.       _moveto(20,(i+2)*fonth);
  215.       _outgtext(buff);
  216.       fgets(buff,MAXLINE,f);
  217.       }
  218.     lastslide=slideno;
  219.     userin();
  220.     while (slideno==lastslide)
  221.       userin();
  222.     fseek(f,slidestart[slideno],SEEK_SET);
  223.     fgets(buff,MAXLINE,f);
  224.     }
  225. }
  226.  
  227. /******************************/
  228. void copviol(void)
  229. /******************************/
  230. {
  231.   _setvideomode(_DEFAULTMODE);
  232.   fprintf(stderr,"\nCOPYRIGHT VIOLATION");
  233.   exit(99);
  234. }
  235.  
  236. /******************************/
  237. void setopts(void)
  238. /******************************/
  239. {
  240.   int modechanged;
  241.   modechanged=0;
  242.   while (buff[0]=='#'&&!feof(f))
  243.     {
  244.     switch (buff[1])
  245.       {
  246.       case 'V':
  247.         switch (toupper(buff[2]))
  248.           {
  249.           case 'V':
  250.             if (vidmode!=_VRES16COLOR)
  251.               {
  252.               vidmode=_VRES16COLOR;
  253.               if (!_setvideomode(vidmode))
  254.                 {
  255.                 _setvideomode(_DEFAULTMODE);
  256.                 fprintf(stderr,"\nVGA video mode not supported");
  257.                 fprintf(stderr,"\nTry #VE at the start of your show");
  258.                 exit(1);
  259.                 }
  260.               else
  261.                 {
  262.                 fonth=43;
  263.                 modechanged=1;
  264.                 vidmode=_VRES16COLOR;
  265.                 strcpy(fonthstr,"43");
  266.                 }
  267.  
  268.               }
  269.             break;
  270.           case 'E':
  271.             if (vidmode!=_ERESCOLOR)
  272.               {
  273.               vidmode=_ERESCOLOR;
  274.               if (!_setvideomode(vidmode))
  275.                 {
  276.                 _setvideomode(_DEFAULTMODE);
  277.                 fprintf(stderr,"\nEGA video mode not supported");
  278.                 fprintf(stderr,"\nTry #VC at the start of your show");
  279.                 exit(1);
  280.                 }
  281.               else
  282.                 {
  283.                 fonth=31;
  284.                 strcpy(fonthstr,"31");
  285.                 modechanged=1;
  286.                 }
  287.               }
  288.             break;
  289.           case 'C':
  290.             if (vidmode!=_HRESBW)
  291.               {
  292.               vidmode=_HRESBW;
  293.               if (!_setvideomode(vidmode))
  294.                 {
  295.                 _setvideomode(_DEFAULTMODE);
  296.                 fprintf(stderr,"\nCGA video mode not supported");
  297.                 fprintf(stderr,"\nI am sorry but you cannot use PCSLIDE");
  298.                 exit(1);
  299.                 }
  300.               else
  301.                 {
  302.                 fonth=18;
  303.                 strcpy(fonthstr,"18");
  304.                 modechanged=1;
  305.                 }
  306.               }
  307.             break;
  308.           default:
  309.             break;
  310.           }
  311.         break;
  312.       case 'H':
  313.         headcolor=atoi(&buff[2]);
  314.         break;
  315.       case 'B':
  316.         bodycolor=atoi(&buff[2]);
  317.         break;
  318.       case 'G':
  319.         _setbkcolor(bgcolors[atoi(&buff[2])%16]);
  320.         break;
  321.       default:
  322.         break;
  323.       }
  324.     fgets(buff,MAXLINE,f);
  325.     }
  326.   /* Have now got video mode         in vidmode
  327.                   color for headings in headcolor
  328.                   color for body     in bodycolor */
  329.   if (vidmode==0)
  330.     {
  331.     _setvideomode(_DEFAULTMODE);
  332.     fprintf(stderr,"\nVGA video mode not supported");
  333.     fprintf(stderr,"\nTry #VE at the start of your show");
  334.     exit(1);
  335.     }
  336.   if (modechanged)
  337.     {
  338.     strcpy(headfont,headbase);
  339.     strcat(headfont,fonthstr);
  340.     strcpy(bodyfont,bodybase);
  341.     strcat(bodyfont,fonthstr);
  342.     }
  343. }
  344.  
  345. /* Resets slideno to number of next slide */
  346. /******************************/
  347. void userin(void)
  348. /******************************/
  349. {
  350.   unsigned int keypress;
  351.  
  352.   /* Wait for a key press */
  353.   while (!kbhit());
  354.  
  355.   /* Get key press */
  356.   keypress=getch();
  357.  
  358.   /* Process extended scan codes */
  359.   if (keypress==0)
  360.     keypress=256*getch();
  361.  
  362.   /* Process escape key */
  363.   if (keypress==27)
  364.     {
  365.     _setvideomode(_DEFAULTMODE);
  366.     exit(0);
  367.     }
  368.   
  369.   switch (keypress)
  370.     {
  371.     /* Up or left - back one slide */
  372.     case 72*256:
  373.     case 75*256:
  374.       slideno-=1;
  375.       break;
  376.  
  377.     /* Home - goto slide 1 */
  378.     case 71*256:
  379.       slideno=0;
  380.       break;
  381.  
  382.     /* End - goto last slide */
  383.     case 79*256:
  384.       slideno=numslides-1;
  385.       break;
  386.  
  387.     /* PgUp - go back 5 slides */
  388.     case 73*256:
  389.       slideno-=5;
  390.       break;
  391.  
  392.     /* PgDn - go forward 5 slides */
  393.     case 81*256:
  394.       slideno+=5;
  395.       break;
  396.    
  397.     /* Any other key goes to next slide */
  398.     default:
  399.       slideno++;
  400.       break;
  401.     }
  402.   if (slideno>=numslides) slideno=numslides-1;
  403.   if (slideno<0) slideno=0;
  404. }
  405.  
  406.